import jsonwebtoken from 'jsonwebtoken'
import { secret } from '../config.js'

export const Auth = async (ctx, next) => {
    const { authorization = '' } = ctx.request.header
    const token = authorization.replace('Bearer ', '')
    try {
        const user = jsonwebtoken.verify(token, secret)
        ctx.state.user = user
    } catch(err) {
        ctx.throw(401, err.message)            
    }
    await next()
}